import ObserverList from './ObserverList';
import Observer from './Observer';

class Subject {
    observers: ObserverList;

    constructor() {
        this.observers = new ObserverList();
    }

    addObserver(observer: Observer) {
        this.observers.add(observer);
    }

    removeObserver(observer: Observer) {
        this.observers.removeIndexAt(this.observers.indexOf(observer))
    }

    notify(context) {
        const count = this.observers.count();

        for(let i = 0; i < count; i++) {
            this.observers.get(i).update(context);
        }
    }
}